package com.chance.cc.crawler.development.command.schedule.douyin;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.development.command.meta.oauth.CrawlerOauthCommand;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bx
 * @date 2020/12/15 0015 13:26
 */
@Data
@AllArgsConstructor
public class DYAuthorInfoImport {


    public static void main(String[] args) throws IOException {
        splitResults();
        devicesImport();
    }

    private static void devicesImport() throws IOException {
        CrawlerOauthCommand crawlerOauthCommand = new CrawlerOauthCommand("192.168.1.215",9599);
        List<Map<String,String>> allUserOauthInfos = new ArrayList<>();
//        oauth(allUserOauthInfos);
        devices(allUserOauthInfos);

        List<Map<String,String>> userOauthInfos = new ArrayList<>();
        for (Map<String, String> userOauthInfo : allUserOauthInfos) {
            if (userOauthInfos.size() > 500){
                HttpPage httpPage = crawlerOauthCommand.addOrUpdateUserOAuthInfo(userOauthInfos,false);
                System.out.println(httpPage.getRawText());
                userOauthInfos.clear();
            }
            userOauthInfos.add(userOauthInfo);
        }
        if (userOauthInfos.size() > 0){
            HttpPage httpPage = crawlerOauthCommand.addOrUpdateUserOAuthInfo(userOauthInfos,false);
            System.out.println(httpPage.getRawText());
        }
    }

    private static void oauth(List<Map<String,String>> allUserOauthInfos) throws IOException {
        List<String> kwLines = IOUtils.readLines(new FileInputStream("D:\\chance\\gitspace\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\schedule\\douyin\\dy_author"), Charset.forName("UTF-8"));

        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#")){
                continue;
            }
            String[] oauthInfos = kwLine.split("\t");
            Map<String,String> authMap = new HashMap<>();
            authMap.put("cookie",oauthInfos[0]);
            authMap.put("category",oauthInfos[1]);
            authMap.put("account",oauthInfos[2]);
            Map<String,String> userOauthInfoMap = new HashMap<>();
            userOauthInfoMap.put("domain","douyin");
            userOauthInfoMap.put("oauthInfo", JSON.toJSONString(authMap));
            System.out.println(JSON.toJSONString(userOauthInfoMap));
            allUserOauthInfos.add(userOauthInfoMap);
        }


    }

    private static void devices(List<Map<String,String>> allUserOauthInfos) throws IOException {
        List<String> kwLines = IOUtils.readLines(new FileInputStream("D:\\chance\\soft\\douyin\\devices\\device_result.txt"), Charset.forName("UTF-8"));

        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#")){
                continue;
            }

            Map<String,String> userOauthInfoMap = new HashMap<>();
            userOauthInfoMap.put("domain","douyin");
            userOauthInfoMap.put("oauthInfo", kwLine);
            System.out.println(JSON.toJSONString(userOauthInfoMap));
            allUserOauthInfos.add(userOauthInfoMap);
        }
    }

    private static void splitResults() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\chance\\soft\\douyin\\devices\\device_result.txt");
        List<String> lines = IOUtils.readLines(new FileInputStream("crawler-dev-bootstrap/src/main/java/com/chance/cc/crawler/development/bootstrap/douyin/devices.txt"), "utf-8");
        int accountCount = (int)(lines.size() * 0.5);
        int searchCount = (int)(lines.size() * 0.3);

        List<String> accountList = new ArrayList<>();
        List<String> searchList = new ArrayList<>();
        List<String> commentList = new ArrayList<>();
        for (String line : lines) {
            if (accountList.size() <= accountCount){
                line = line.replaceAll("device_comment","device_account");
                accountList.add(line);
            } else if (searchList.size() <= searchCount){
                line = line.replaceAll("device_comment","device_search");
                searchList.add(line);
            } else {
                commentList.add(line);
            }
        }

        System.out.println("account count :" + accountList.size());
        System.out.println("search count :" + searchList.size());
        System.out.println("comment count :" + commentList.size());

        IOUtils.writeLines(accountList,"\r\n",fileOutputStream,"utf-8");
        IOUtils.writeLines(searchList,"\r\n",fileOutputStream,"utf-8");
        IOUtils.writeLines(commentList,"\r\n",fileOutputStream,"utf-8");

    }
}

